﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Einstein.Data;

namespace Einstein.Group.DAL
{
    public class OperatorDAL
    {

        public IList<Operator> SelectAll()
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.Operator.ToList<Operator>();
            }
        }

        public bool Add(Operator info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                entities.Operator.AddObject(info);
                return entities.SaveChanges() > 0;
            }
        }


        public bool Exist(Operator info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.Operator.Count(a => a.UserName == info.UserName && a.UserID != info.UserID) > 0;
            }
        }

        public bool Update(Operator info)
        {

            using (EinsteinEntities entities = new EinsteinEntities())
            {

                foreach (Operator item in entities.Operator.Where(a => a.UserID == info.UserID))
                {
                    item.ChineseName = info.ChineseName;
                    item.Password = info.Password;
                    item.ExpireDate = info.ExpireDate;
                    item.RoleId = item.RoleId;
                    item.DeptId = item.DeptId;
                    item.ModifyDate = info.ModifyDate;
                    item.ModifyBy = info.ModifyBy;

                }
                return entities.SaveChanges() > 0;
            }
        }

        public bool Delete(Operator info)
        {
            using (EinsteinEntities context = new EinsteinEntities())
            {
                foreach (var item in context.Operator.Where(a => a.UserID == info.UserID))
                {
                    context.DeleteObject(item);
                }
                return context.SaveChanges() > 0;
            }
        }

        public Operator GetByUserNameAndPwd(Operator info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.Operator.FirstOrDefault(a => a.UserName == info.UserName && a.Password == info.Password);
            }
        }
    }
}
